package com.example.CarFee;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.trainfee.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class DiscussActivity extends Activity {
	private ListView listView;
	private ImageView backBtn, refreshBtn;
	private SimpleAdapter adapter;
	private List<Map<String, String>> items = new ArrayList<Map<String, String>>();
	private String[] contentList;
	private TextView pinglun;
	private Button fabuBtn;
	private EditText fabuContent;
	private ProgressDialog bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discuss);
		listView = (ListView) findViewById(R.id.listView);
		backBtn = (ImageView) findViewById(R.id.backBtn);
		refreshBtn = (ImageView) findViewById(R.id.refresh);
		fabuContent = (EditText) findViewById(R.id.fabucontent);
		fabuBtn = (Button) findViewById(R.id.fabuBtn);
		bar = new ProgressDialog(this);
		bar.setTitle("发布中");
		bar.setMessage("请稍后");
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				Intent intent = new Intent(DiscussActivity.this,
						PinlunActivity.class);
				intent.putExtra("content", contentList[contentList.length - pos
						- 1]);
				startActivity(intent);
			}
		});
		refreshBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				bar.show();
				items = new ArrayList<Map<String, String>>();
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						contentList = Service.RefreshInfoList();
						handler.sendEmptyMessage(0x120);
					}
				});
				thread.start();
			}
		});
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				contentList = Service.RefreshInfoList();
				handler.sendEmptyMessage(0x120);
			}
		});
		thread.start();
		fabuBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (!fabuContent.getText().toString().equals("")) {
					bar.show();
					items = new ArrayList<Map<String, String>>();
					Thread thread = new Thread(new Runnable() {

						@Override
						public void run() {
							String content = "";
							try {
								content = URLEncoder.encode(fabuContent
										.getText().toString(), "UTF-8");
							} catch (UnsupportedEncodingException e) {
								e.printStackTrace();
							}
							Service.Release(content);
							handler.sendEmptyMessage(0x121);
							contentList = Service.RefreshInfoList();
							handler.sendEmptyMessage(0x120);
						}
					});
					thread.start();
				} else {
					Toast.makeText(DiscussActivity.this, "发送内容不能为空",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			bar.cancel();
			if (msg.what == 0x120) {
				for (int i = 0; i < contentList.length; i++) {
					HashMap<String, String> item = new HashMap<String, String>();
					item.put("content", contentList[contentList.length - i - 1]);
					item.put("pinglun", "评论");
					items.add(item);
				}
				adapter = new SimpleAdapter(DiscussActivity.this, items,
						R.layout.discuss_listview_item, new String[] {
								"content", "pinglun" }, new int[] {
								R.id.content, R.id.pinglun });
				listView.setAdapter(adapter);
			}
			if (msg.what == 0x121) {
				Toast.makeText(DiscussActivity.this, "发送成功", Toast.LENGTH_SHORT)
						.show();
				fabuContent.setText("");
			}
		}
	};
}
